package top.soulblack.purchasesale.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.javafx.collections.MappingChange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.soulblack.purchasesale.common.enums.ResultEnum;
import top.soulblack.purchasesale.exception.PurchaseException;
import top.soulblack.purchasesale.model.VO.StoreManagementVO;
import top.soulblack.purchasesale.mapper.StoreHouseMapper;
import top.soulblack.purchasesale.model.StoreHouse;
import top.soulblack.purchasesale.model.User;


import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 韦溜溜
 * @Description:仓库管理接口实现类
 * @Date: Create in 13:46 2019/5/19
 */
@Service
@Slf4j
public class StoreHouseService {


    @Autowired
    private StoreHouseMapper storeHouseMapper;

    @Autowired
    private UserService userService;


    public List<StoreHouse> findAll() {
        return storeHouseMapper.selectList();
    }

    public Boolean addStore(StoreHouse storeHouse) {
       Integer num = storeHouseMapper.insert(storeHouse);
       if (num == 1){
           return true;
       }
       else{
           return false;
       }
    }

    public Boolean delectStoreById(Integer storeId) {
       Integer num =  storeHouseMapper.deleteById(storeId);
        if (num == 1){
            return true;
        }
        else{
            return false;
        }
    }


    public StoreHouse findOneById(Integer storeId) {
        StoreHouse storeHouse = storeHouseMapper.selectById(storeId);
        if (storeHouse==null){
            log.error("【查询仓库】通过shopId查询,storeId={}",storeId);
            throw new PurchaseException(ResultEnum.STOREHOUSEEMPITY);
        }
        return storeHouse;
    }


    public Boolean updateStore(StoreHouse storeHouse) {
        Integer num = storeHouseMapper.updateById(storeHouse);
        if (num == 1){
            return true;
        }
        else{
            return false;
        }
    }


    public PageInfo<StoreManagementVO> getVoPage(Integer page,Integer size) {
        //需要把放在语句调用mapper前面，不然不生效.
        PageHelper.startPage(page,size);
        //查询仓库表
        List<StoreHouse> storeHouses = storeHouseMapper.selectList();
        //创建分页对象
        PageInfo pageInfo = new PageInfo(storeHouses);
        //查询用户表
        List<User> users = userService.findAll();
        //拼装数据
        List<StoreManagementVO> storeManagementVOS = storeHouses.stream().map(e ->{
                    StoreManagementVO storeManagementVO = new StoreManagementVO();
                    BeanUtils.copyProperties(e,storeManagementVO);
                    users.forEach(user->{
                        if (user.getUserId().equals(e.getUserId())){
                            storeManagementVO.setUserName(user.getUserName());
                        }
                    });
                    //TODO 有门店表之后才能进行数据拼装
                    return storeManagementVO;
                }
        ).collect(Collectors.toList());
        //对VO进行分页
        pageInfo.setList(storeManagementVOS);
        return pageInfo;
    }

    public List<StoreHouse> selectByShopId( Integer shopId){
        List<StoreHouse> storeHouseList = storeHouseMapper.selectByShopId(shopId);
        if(storeHouseList.size()==0){
            log.error("【查询仓库】通过shopId查询,shopId{}",shopId);
            throw new PurchaseException(ResultEnum.STOREHOUSEEMPITY);
        }
        return storeHouseList;
    }

    public Map<Integer,StoreHouse> getStoreToMap(){
        List<StoreHouse> storeHouses = storeHouseMapper.selectList();
        Map<Integer,StoreHouse> map = new HashMap<>();
        storeHouses.stream().forEach(e->{
            map.put(e.getStoreId(),e);
        });
        return map;
    }

}
